from PyQt5.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QLabel, 
                         QProgressBar, QScrollArea, QWidget, QTextEdit, QPushButton)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt, pyqtSignal, QSize

from meet_record.ui_components import MarkdownHighlighter

# 会议记录总结对话框
class SummaryDialog(QDialog):
    # 定义信号
    update_progress = pyqtSignal(int)
    show_result = pyqtSignal(str)
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("会议记录分析报告")
        self.setFixedSize(600, 500)
        self.setStyleSheet("""
            QDialog {
                background-color: #FFFFFF;
                border: 1px solid #2196F3;
            }
        """)
        
        layout = QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        
        # 标题栏
        title_bar = QWidget()
        title_bar.setFixedHeight(40)
        title_bar.setStyleSheet("background-color: #2196F3;")
        title_layout = QHBoxLayout(title_bar)
        
        self.title_label = QLabel("会议记录分析报告")
        self.title_label.setStyleSheet("color: #FFFFFF; font-weight: bold; font-size: 16px;")
        title_layout.addWidget(self.title_label)
        
        # 使用内联SVG作为图标，避免文件路径问题
        self.close_btn = QPushButton(self)
        self.close_btn.setFixedSize(30, 30)
        self.close_btn.setStyleSheet("background-color: transparent; border: none;")
        self.close_btn.setIcon(QIcon("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23F9AA33' stroke-width='2'%3E%3Cpath d='M18 6L6 18M6 6l12 12'/%3E%3C/svg%3E"))
        self.close_btn.setIconSize(QSize(20, 20))
        self.close_btn.setToolTip("关闭")
        self.close_btn.clicked.connect(self.close)
        title_layout.addWidget(self.close_btn)
        
        layout.addWidget(title_bar)
        
        # 内容区域
        self.content_area = QScrollArea()
        self.content_area.setWidgetResizable(True)
        self.content_area.setStyleSheet("background-color: #FFFFFF; border: none;")
        
        self.content_widget = QWidget()
        self.content_layout = QVBoxLayout(self.content_widget)
        self.content_layout.setContentsMargins(20, 20, 20, 20)
        
        # 加载状态
        self.loading_container = QWidget()
        self.loading_container.setFixedHeight(150)
        loading_layout = QVBoxLayout(self.loading_container)
        loading_layout.setAlignment(Qt.AlignCenter)
        
        self.loading_label = QLabel("正在分析会议记录，请稍候...")
        self.loading_label.setStyleSheet("color: #2196F3; font-size: 14px;")
        
        self.progress_bar = QProgressBar()
        self.progress_bar.setFixedWidth(300)
        self.progress_bar.setStyleSheet("""
            QProgressBar {
                border: 1px solid #F9AA33;
                border-radius: 5px;
                text-align: center;
                color: white;
            }
            QProgressBar::chunk {
                background-color: #F9AA33;
                width: 10px;
            }
        """)
        
        loading_layout.addWidget(self.loading_label)
        loading_layout.addWidget(self.progress_bar)
        
        self.result_text = QTextEdit()
        self.result_text.setReadOnly(True)
        self.result_text.setStyleSheet("""
            QTextEdit {
                background-color: transparent;
                border: none;
                color: #333333;
                font-size: 14px;
            }
        """)
        self.result_text.setVisible(False)
        
        self.content_layout.addWidget(self.loading_container)
        self.content_layout.addWidget(self.result_text)
        self.content_area.setWidget(self.content_widget)
        
        layout.addWidget(self.content_area)
        self.setLayout(layout)
        
        # 连接信号
        self.update_progress.connect(self._update_progress)
        self.show_result.connect(self._show_result)
    
    def show_loading(self):
        """显示加载状态"""
        self.loading_container.setVisible(True)
        self.result_text.setVisible(False)
        self.progress_bar.setValue(0)
    
    def _show_result(self, result):
        """显示分析结果（信号槽）"""
        self.show_result_impl(result)
    
    def show_result_impl(self, result):
        """显示分析结果实现"""
        self.loading_container.setVisible(False)
        self.result_text.setVisible(True)
        
        # 设置Markdown格式
        self.result_text.setMarkdown(result)
        
        # 使用自定义高亮器
        highlighter = MarkdownHighlighter(self.result_text.document())
        
        # 确保对话框在显示结果时保持足够高度
        self.setFixedHeight(550)
    
    def _update_progress(self, value):
        """更新进度条（信号槽）"""
        self.progress_bar.setValue(value)

# 这里不需要重复导入QTextEdit，已经在顶部导入